---@diagnostic disable: undefined-global

--- 挥泪技能
--- 你死亡时，可令任意名有明置牌的角色弃置其所有非明置手牌。
local huilei = fk.CreateSkill{
  name = "lvshi__huilei",
  frequency = "compulsory",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"

huilei:addEffect(fk.Death, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(huilei.name, true, true)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(), function(p)
      return #DIY.getShownCards(p) > 0
    end)
    -- 选择任意名有明置牌的角色
    local selected = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 999,
      prompt = "#ls__huilei-choose",
      skill_name = huilei.name,
    })
    if #selected > 0 then
      event:setCostData(self, {tos = selected})
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos
    
    for _, p in ipairs(targets) do
      local to_discard = {}
      -- 找到所有非明置的手牌
      for _, card_id in ipairs(p:getCardIds("h")) do
        if not table.contains(DIY.getShownCards(p), card_id) then
          table.insert(to_discard, card_id)
        end
      end
      -- 弃置所有非明置手牌
      if #to_discard > 0 then
        room:throwCard(to_discard, huilei.name, p, p)
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["lvshi__huilei"] = "挥泪",
  [":lvshi__huilei"] = "你死亡时，可令任意名有明置牌的角色弃置其所有非明置手牌。",
  
  ["#ls__huilei-choose"] = "挥泪：选择有明置牌的角色，其弃置所有非明置手牌",
  
  ["$lvshi__huilei1"] = "丞相保重，马谡先行一步了。",
  ["$lvshi__huilei2"] = "谡虽死，无恨于九泉。",
}

return huilei